package com.nobuyme.box.toy.manage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nobuyme.box.R;
import com.nobuyme.box.account.Account;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.app.UrlCenter;
import com.nobuyme.box.toy.ToyListFragment;
import com.nobuyme.box.toy.ToyManageListActivity;
import com.nobuyme.box.util.UICommon;
import com.nobuyme.box.xunfei.XunFeiSpeech;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import android_serialport_api.SerialPortFinder;
import butterknife.Bind;
import butterknife.OnClick;

public class OffGoodsActivity extends BaseActivity {

    @Bind(R.id.tv_off_goods)
    TextView mTvOffGoods;
    @Bind(R.id.tv_off_id)
    TextView mTvOffId;
    @Bind(R.id.iv_off_goods)
    ImageView mIvOffGoods;
    @Bind(R.id.tv_off_state)
    TextView mTvOffState;
    private int position;
    private SerialPortFinder mSerialPortFinder;
    private ToyListFragment.TestBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
        data = (ToyListFragment.TestBean) getIntent().getSerializableExtra("data");
//        EasyToast.showToast(getApplicationContext(),data.toString()+data.getLattice_id());
        position = getIntent().getIntExtra("position", -1);
        initData();
    }

    private void initData() {
        mSerialPortFinder = UICommon.getInstance().getSerialPortFinder();
        mTvOffState.setText(position + "号储物格已打开");
        Glide.with(this).load(data.getCover()).into(mIvOffGoods);
        mTvOffGoods.setText(data.getName());
        mTvOffId.setText("商品ID:" + data.getGoods_id());

        try {
            XunFeiSpeech xunFeiSpeech = new XunFeiSpeech(this);
            xunFeiSpeech.speak("12", position + "号储物格已打开，取出商品后请关闭柜门", new XunFeiSpeech.OnCompleteListener() {
                @Override
                public void onComplete() {
//                    EasyToast.showToast(PutGoodsActivity.this,"柜门已打开");
                }
            });
        } catch (NullPointerException e) {
        }
        //万科柜子、串口1的 17打不开。18打开的是17     19打开18
        Account acc = AccountManager.getCurrentAccount();
        //下架开柜
        PutGoodsActivity.openLattice(acc.getId(), acc.getPolling_one(), acc.getPolling_two(), acc.getPolling_three(), acc.getPolling_four(), position);

    }

    public static void start(Activity activity, ToyListFragment.TestBean data, int position) {
        Intent intent = new Intent(activity, OffGoodsActivity.class);
        intent.putExtra("data", data);
        intent.putExtra("position", position);
        activity.startActivity(intent);
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_off_goods, null);
    }

    @OnClick({R.id.btn_off_complete, R.id.btn_off_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_off_complete:
                setStateId();
                break;
            case R.id.btn_off_cancel:
                ToyManageListActivity.start(OffGoodsActivity.this,1);
                finish();
                break;
        }
    }

    private void setStateId() {
        HttpRequest request = new HttpRequest(this);
        String s = String.format(UrlCenter.PUT_GOODS, data.getLattice_id());
        String urlPut = s + "?type=3";
        request.put(urlPut, null, new DataRequestListener<Object>(Object.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                EasyToast.showToast(OffGoodsActivity.this, "下架成功");
                ToyManageListActivity.start(OffGoodsActivity.this,1);
                finish();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(getApplicationContext(),strMsg);
            }
        });
    }
}
